package com.fenghuajueli.libbasecoreui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.fenghuajueli.libbasecoreui.R;
import com.fenghuajueli.libbasecoreui.data.entity.base.MenuEntity;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;

import java.util.List;


public class CustomMenuAdapter extends RecyclerView.Adapter<CustomMenuAdapter.ViewHolder> {

    private Context mContext;
    private List<MenuEntity> menuEntityList;
    private OnListClickListener<MenuEntity> onListItemClickListener;
    private String fatherName = "";
    private String mainMenuName = "";//第一级的菜单名称

    public CustomMenuAdapter(Context mContext, List<MenuEntity> menuEntityList) {
        this.mContext = mContext;
        this.menuEntityList = menuEntityList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.custom_menu_item_layout, parent, false);
        return new ViewHolder(view);
    }

    public void setOnListItemClickListener(OnListClickListener<MenuEntity> onListItemClickListener) {
        this.onListItemClickListener = onListItemClickListener;
    }

    public String getFatherName() {
        return fatherName;
    }

    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

    public String getMainMenuName() {
        return mainMenuName;
    }

    public void setMainMenuName(String mainMenuName) {
        this.mainMenuName = mainMenuName;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final MenuEntity menuEntity = menuEntityList.get(position);
        holder.tvMenuName.setText(menuEntity.getName());
        if (menuEntity.isAvailable()) {
            holder.ivMenuIcon.setColorFilter(null);
            holder.tvMenuName.setTextColor(Color.WHITE);
            holder.itemContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onListItemClickListener != null) {
                        onListItemClickListener.itemClick(position, menuEntity);
                    }
                }
            });
        } else {
            holder.ivMenuIcon.setColorFilter(Color.parseColor("#999999"));
            holder.itemContainer.setOnClickListener(null);
            holder.tvMenuName.setTextColor(Color.parseColor("#999999"));
        }
        holder.ivMenuIcon.setImageResource(menuEntity.getResIcon());
    }

    public void refreshData(List<MenuEntity> childList) {
        this.menuEntityList = childList;
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return menuEntityList.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView ivMenuIcon;
        public TextView tvMenuName;
        public LinearLayout itemContainer;

        public ViewHolder(View itemView) {
            super(itemView);
            ivMenuIcon = itemView.findViewById(R.id.ivMenuIcon);
            tvMenuName = itemView.findViewById(R.id.tvMenuName);
            itemContainer = itemView.findViewById(R.id.itemContainer);
        }


    }
}
